<?php

/**
 * 模型基类，可以链接数据库，并且可以查询SQL，并返回数据集
 */
class Model {

    protected $dbh;

    public function __construct() {
        $this->connect();
    }

    /**
     * 链接数据库
     */
    public function connect() {
        $config = include APP_PATH . "config.php";
        $this->dbh = new PDO("mysql:host={$config['host']};dbname={$config['dbname']}", $config['user'], $config['pass']);
    }

    /**
     * 查询SQL,并且返回结果集
     */
    public function query($sql, $all = true) {
        $sth = $this->dbh->query($sql);
        if ($all == false) {
            $row = $sth->fetch(PDO::FETCH_ASSOC);
        } else {
            $row = $sth->fetchAll(PDO::FETCH_ASSOC);
        }
        return $row;
    }
    
    /**
     * 执行SQL操作
     * 
     * @param type $sql
     * @return type
     */
    public function execute($sql) {
        $count = $this->dbh->exec($sql);
        return $count;
        
    }

}
